////////////////////////////////////////////////////////////////////////////////
//                                                                            //
//                           BailterRay                                       //
//                    Pip Robbins & Zhao Zhang                                //
//                                                                            //
////////////////////////////////////////////////////////////////////////////////
class BailterRayAttachment_RayGun extends UTWeaponAttachment;

/** component for the laser effect */
var ParticleSystemComponent LaserEffect;
var ParticleSystem RedGlow;
var UTEmitter LaserPointEmitter;

simulated function AttachTo(UTPawn OwnerPawn)
{
    Super.AttachTo(OwnerPawn);

	if(OwnerPawn.IsHumanControlled())
	{

        if (LaserEffect != None)
    	{
    		Mesh.AttachComponentToSocket(LaserEffect, 'RayStartBeam');

    	}
    	LaserPointEmitter = Spawn(class'UTEmitter', self);
		LaserPointEmitter.SetBase(self,, Self.Mesh);
		LaserPointEmitter.SetTemplate(RedGlow, true);
		LaserPointEmitter.LifeSpan = 0;
     }
}

simulated function DetachFrom( SkeletalMeshComponent MeshCpnt )
{
    LaserPointEmitter.Destroy();
    Super.DetachFrom(MeshCpnt);
}

simulated function Tick(float DeltaTime)
{
	Super.Tick(DeltaTime);
	UpdateTargetingLaser();
}

function ThirdPersonFireEffectsLaser(vector HitLocation)
{
        // firing targeting laser
		LaserEffect.SetActive(true);
		LaserEffect.SetVectorParameter('BeamEnd', HitLocation);
        LaserPointEmitter.SetLocation(HitLocation);
}

simulated function UpdateTargetingLaser()
{
    local BailterRayPawn P;
	local vector TargetLocation, StartTrace, EndTrace;
	local ImpactInfo Impact;

    P = BailterRayPawn(Instigator);

    // Only Play laser effect for human
    if(!P.IsHumanControlled())
        return;
    if(P.Controller.GamePlayEndedState())
    {
        LaserPointEmitter.Destroy();
        return;
    }
    if(P.IsInState('FeigningDeath'))
    {
        LaserPointEmitter.SetHidden(true);
        LaserEffect.SetActive(false);
        return;
    }

    LaserPointEmitter.SetHidden(false);

    Mesh.GetSocketWorldLocationAndRotation('RayStartBeam', StartTrace);

	// we have to trace to find out what we're hitting
	EndTrace = ((StartTrace + vector(P.GetViewRotation()) * 22000)); // + (P.Velocity * 3));

	Impact = P.Weapon.CalcWeaponFire(StartTrace, EndTrace);
	TargetLocation = Impact.HitLocation;

    ThirdPersonFireEffectsLaser(TargetLocation);

}

defaultproperties
{

   Begin Object Class=ParticleSystemComponent Name=LaserComp ObjName=LaserComp Archetype=ParticleSystemComponent'Engine.Default__ParticleSystemComponent'
      Template=ParticleSystem'WP_AVRiL.Particles.P_WP_AVRiL_TargetBeam'
      bAutoActivate=False
      bUpdateComponentInTick=True
      TickGroup=TG_PostAsyncWork
      Name="LaserComp"
      ObjectArchetype=ParticleSystemComponent'Engine.Default__ParticleSystemComponent'
   End Object
   LaserEffect=LaserComp

   RedGlow = ParticleSystem'CTF_Flag_IronGuard.Effects.P_CTF_Flag_IronGuard_Idle_Red'

   // 3rd Person Mesh
   Begin Object  Name=SkeletalMeshComponent0
      SkeletalMesh=SkeletalMesh'BailterRay_RayGun.RayGun'
      Scale = 1.5
      Translation = (X = 0, Y  = 13.0, Z = 5.0);
   End Object
   Mesh=SkeletalMeshComponent0

   MuzzleFlashSocket="MussleFlashSocket"
   MuzzleFlashPSCTemplate=ParticleSystem'BailterRay_RayGun.RayGun_MF'
   MuzzleFlashLightClass=Class'BailterRay_RayMuzzleLight'
   MuzzleFlashDuration=0.2
   bMuzzleFlashPSCLoops=false
   WeapAnimType=EWAT_Pistol

   AltFireAnim = none
   MuzzleFlashAltPSCTemplate = none

   WeaponClass=Class'BailterRay.BailterRayWeap_RayGun'
   Name="Default__BailterRayAttachment_RayGun"
}
